 //
//  EmoticonPackage.m
//  Emoticon
//
//  Created by 袁量 on 2017/8/12.
//  Copyright © 2017年 袁量. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "EmoticonPackage.h"
#import "Emoticon.h"
@interface EmoticonPackage()

@end
@implementation EmoticonPackage
- (id)initWithid:(NSString *)id{
    if(self = [super init]){
        if([id  isEqual: @""]){
            [self addEmptyEmoticon:YES];
        }
        //根据id拼接plist路径
        NSString *str  = [id stringByAppendingPathComponent:@"info.plist"];
        NSString *path = [NSBundle.mainBundle pathForResource:str ofType:nil inDirectory:@"Emoticons.bundle"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        int index = 0;
        for (NSDictionary *dict in array){
            NSString *png = [dict objectForKey:@"png"];
//            dict[@"png"] = [id stringByAppendingPathComponent:png];
            [dict setValue:[id stringByAppendingPathComponent:png] forKey:@"png"];
            [self.emoticons addObject:[[Emoticon alloc] initWithDict:dict]];
            index ++;
            if (index == 20){
                [self.emoticons addObject:[[Emoticon alloc] initWithIsRemove:YES]];
                index = 0;
            }
        }
        [self addEmptyEmoticon:NO];
        
    }
    return self; 
}
- (void)addEmptyEmoticon:(BOOL)isRecently{
    int countM = self.emoticons.count % 21;
    if (countM == 0 && !isRecently) {
        return;
    }
    for (int i = countM;i < 20;i++){
        [self.emoticons addObject:[[Emoticon alloc] initWithIsEmpty:YES]];
    }
    [self.emoticons addObject:[[Emoticon alloc] initWithIsRemove:YES]];
}
-(NSMutableArray *)emoticons{
    if (!_emoticons) {
        _emoticons = [NSMutableArray array];
    }
    return _emoticons;
}
@end
